package cn.epsilon3.common;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import cn.epsilon3.domain.e3.vo.E3ResourceVO;
import cn.epsilon3.exception.Epsilon3ReturnException;
import cn.epsilon3.utils.SpringContextUtils;
import com.github.benmanes.caffeine.cache.Cache;
import net.jodah.expiringmap.ExpiringMap;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @description 封装service层常用方法
 *
 * @author: epsilon3
 * @date: 2023/11/07 23:10
 */
public class E3BaseService {

    /**
     * 获取Caffeine实例
     *
     * @return Cache<String, Object>
     */
    @SuppressWarnings("unchecked")
    public static Cache<String, Object> initCaffeineCache() {
        return (Cache<String, Object>) SpringContextUtils.getBean("E3CaffeineCache");
    }

    /**
     * 获取可达路径缓存
     *
     */
    @SuppressWarnings("unchecked")
    public static ConcurrentHashMap<String,Integer> getUrl() {
        return (ConcurrentHashMap<String,Integer>) SpringContextUtils.getBean("loadUrlCache");
    }

    /**
     * 获取资源树缓存
     *
     */
    @SuppressWarnings("unchecked")
    public static List<E3ResourceVO> getLoadResourceTreeCache(String roleId) {
        ConcurrentHashMap<String, List<E3ResourceVO>> loadMenuTreeCache = (ConcurrentHashMap<String, List<E3ResourceVO>>) SpringContextUtils.getBean("loadMenuTreeCache");
        if (loadMenuTreeCache.containsKey(roleId)) {
            return loadMenuTreeCache.get(roleId);
        } else {
            throw new Epsilon3ReturnException("此角色暂未配置菜单树");
        }
    }

    /**
     * 获取黑名单ip缓存
     *
     */
    @SuppressWarnings("unchecked")
    public static ExpiringMap<String, Integer> getBlackIpCache() {
        return (ExpiringMap<String, Integer>) SpringContextUtils.getBean("loadBlackIpCache");
    }


    /**
     * 初始化ConcurrentHashMap
     *
     * @param keyClass key类型
     * @param valueClass value类型
     * @return ConcurrentHashMap实例
     * @param <K> key类型
     * @param <V> value类型
     */
    public static <K, V> ConcurrentHashMap<K, V> initConcurrentHashMap(Class<K> keyClass, Class<V> valueClass) {
        return new ConcurrentHashMap<>(16);
    }

    /**
     * 初始化ConcurrentHashMap,value类型为List
     *
     * @param keyClass key类型
     * @param valueClass value类型
     * @return ConcurrentHashMap实例
     * @param <K> key类型
     * @param <V> value类型
     */
    public static <K, V> ConcurrentHashMap<K, List<V>> initConcurrentHashMapForValueList(Class<K> keyClass, Class<V> valueClass) {
        return new ConcurrentHashMap<>(16);
    }

    /**
     * 初始化HashMap
     *
     * @param keyClass key类型
     * @param valueClass value类型
     * @return HashMap实例
     * @param <K> key类型
     * @param <V> value类型
     */
    public static <K, V> HashMap<K, V> initHashMap(Class<K> keyClass, Class<V> valueClass) {
        return new HashMap<>(16);
    }

    /**
     * 初始化ExpiringMap
     *
     * @param keyClass key类型
     * @param valueClass value类型
     * @return ExpiringMap实例
     * @param <K> key类型
     * @param <V> value类型
     */
    public static <K, V> ExpiringMap<K, V> initExpiringMap(Class<K> keyClass, Class<V> valueClass) {
        return ExpiringMap.builder().variableExpiration().build();
    }

    /**
     * 获取对应实体的LambdaQueryWrapper
     *
     * @param clazz 实体class
     * @return LambdaQueryWrapper
     * @param <T> 泛型
     */
    public static <T> LambdaQueryWrapper<T> queryWrappers(Class<T> clazz) {
        return Wrappers.<T>lambdaQuery(clazz);
    }

    /**
     * LambdaUpdateWrapper
     *
     * @param clazz 实体class
     * @return LambdaUpdateWrapper
     * @param <T> 泛型
     */
    public static <T> LambdaUpdateWrapper<T> updateWrappers(Class<T> clazz) {
        return Wrappers.<T>lambdaUpdate(clazz);
    }
}
